//
// Created by antler on 2018/12/6.
//

#ifndef LUA_IMAGE_H
#define LUA_IMAGE_H

#include <string>

#include <android/bitmap.h>

#include "ImageUtils.h"

#include "luaext/point/Point.h"
#include "luaext/size/Size.h"
#include "luaext/rect/Rect.h"
#include "luaext/color3b/Color3B.h"

#define CLASSNAME_IMAGE "Image"
#define USERTYPE_IMAGE "Image"

class Image {
public:
    enum {
        ROTATION_CLOCKWISE_0 = 0,
        ROTATION_CLOCKWISE_90 = 1,
        ROTATION_CLOCKWISE_180 = 2,
        ROTATION_CLOCKWISE_270 = 3,
    };

    enum IMAGE_FROM_TYPE {
        IMAGE_FROM_TYPE_KNOWN = -1,
        IMAGE_FROM_TYPE_SCREEN,
        IMAGE_FROM_TYPE_FILE,
    };

public:
    // 将屏幕rect范围的图像截取成Image对象并返回
    static Image *fromScreen(const Rect &rect = Rect::ZERO);

    // 从文件构造图像实例
    static Image *fromFile(const std::string &path);

    ~Image();

    // 释放图像
    void release();

    // 获取图像尺寸
    Size getSize();

    // 获取图像尺寸-宽
    int getWidth();

    // 获取图像尺寸-高
    int getHeight();

    // 旋转图像
    void setRotation(float degree);

    // 把图像实例保存到本地
    bool saveToFile(const std::string &filePath, int quality = 100);

    // 在当前Image截取保留rect范围的像素
    void clip(const Rect &rect);

    // 获取图像颜色值
    bool getColor(const Point &pos, Color3B &c3b);

    // 图像二值化成
    Image *binarize();

    jobject getBitmapObject();

    std::string getImageFromTypeName();

    void *lockPixels();

    void unlockPixels();

    bool getPixel(void *addrPtr, int x, int y, Color3B &c3b);

protected:
    Image();

    bool initWith(jobject jBitmap, IMAGE_FROM_TYPE imageFromType);

    bool isOk();

    void setBitmapObject(jobject jBitmap, IMAGE_FROM_TYPE imageFromType);

    void releaseBitmapObject();

    void setImageFromType(IMAGE_FROM_TYPE imageFromType);

protected:
    IMAGE_FROM_TYPE mImageFromType;
    jobject mjBitmap;
    AndroidBitmapInfo mBitmapInfo;
};


#endif //LUA_IMAGE_H
